package com.gexin.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gexin.common.cache.CacheUtils;
import com.gexin.model.weixin.WeixinAppConfig;
import com.jfinal.kit.HttpKit;
import com.jfinal.log.Log;

/**
 * 微信公众号Token 工具类
 *
 * @author 肖得明 2016 05 27
 */
public class WexinAccessTokenKit{
	static final Log log = Log.getLog(WexinAccessTokenKit.class);

	private static final ThreadLocal<Integer> tl = new ThreadLocal<Integer>();

	public static void setThreadLocalShopId(Integer wuye_id) {
		tl.set(wuye_id);
	}

	public static void removeThreadLocalWuYeId() {
		tl.remove();
	}


	public static String getAccessTokenToStr() {
	 WeixinAppConfig weixinAppConfig =WeixinAppConfig.dao.findById(2);
	 String appid= weixinAppConfig.getAppId();
	 String secret= weixinAppConfig.getAppSecret();
		String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;
//		String accesstoken= CacheUtils.get("ZhongKeAccessToken");
//		if(StringUtil.isBlank(accesstoken)){
			String doGet = HttpKit.get(requestUrl);
			JSONObject jsonObject = JSON.parseObject(doGet);
		String accesstoken = jsonObject.getString("access_token");
//			CacheUtils.set("ZhongKeAccessToken",accesstoken,7200);
//		}
//		log.info("token:"+accesstoken);
		return accesstoken;
	}


}
